package practice;

/**
 * @Description: https://leetcode.cn/problems/powx-n/
 * @Author Ammar
 * @Create 2023/7/20 20:05
 */
public class _50_Pow {
    public static void main(String[] args) {
        new _50_Pow().myPow(2, 10);
    }

    public double myPow(double x, int n) {
        long a = n < 0 ? -((long) n) : n;
        double res = 1.0;
        while (a > 0) {
            if ((a & 1) == 1) {
                res *= x;
            }
            x *= x;
            a >>= 1;
        }
        return n < 0 ? (1 / res) : res;
    }

    public double myPow1(int x, int n, int z) {
        if (n < 0 || z <= 0) return 0;
        int res = 1 % z;
        x = x % z;
        while (n > 0) {
            if ((n & 1) == 1) {
                res = (x * res) % z;
            }
            x = (x * x) % z;
            n >>= 1;
        }
        return res;
    }
}
